<?php
// 本类由系统自动生成，仅供测试用途
class PacketAction extends CommonAction {
	
	/**
	 * 发红包列表
	 */
	public function hair_packet_list(){
		if(!empty($_POST['uname'])){
			$chaxun=M("User")->where("username='{$_POST['uname']}'")->find();
			$where['uid']=$chaxun['id'];
		}
		
		$Packet = M('Packet'); // 实例化User对象
		import('ORG.Util.Page');// 导入分页类
		$count      = $Packet->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $Packet->where($where)->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($list as $k=>$v){
			$user=M("User")->where("id='{$v['uid']}'")->find();
			$list[$k]['username']=$user['username'];
			$count=M("News")->where("packet_id='{$v['id']}'")->count();
	
			$list[$k]['new_num']=$v['num']-$count;
		}
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		
		$this->display('./Tpl/Admin/Packet_hair.html');
	}
	/**
	 * 发红包列表的删除方法
	 */
	public function hair_packet_del(){
		$id=intval($_GET['id']);
		$re=M("Packet")->where("id='$id'")->delete();
		if($re){
			$this->success("删除成功");
		}else{
			$this->error("删除失败");
		}
		
	}
	
	public function collect_packet_list(){
		if(!empty($_POST['uname'])){
			$chaxun=M("User")->where("username='{$_POST['uname']}'")->find();
			$where['uid']=$chaxun['id'];
		}
		
		$News = M('News'); // 实例化User对象
		import('ORG.Util.Page');// 导入分页类
		$count      = $News->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $News->where($where)->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($list as $k=>$v){
			$user=M("User")->where("id='{$v['uid']}'")->find();
			$list[$k]['username']=$user['username'];
		
		}
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		
		$this->display('./Tpl/Admin/Packet_collect.html');
	}
	
	/**
	 * 添加红包页面
	 */
	public function fafang_packet(){
		$this->display('./Tpl/Admin/fafang_packet.html');
	}
	public function fafang_packet_ok(){
		if(empty($_POST['num'])){
			$this->error("请输入红包份数");
		}
		if(empty($_POST['money'])){
			$this->error("请输入一共发多少个币");
		}
		if(empty($_POST['content'])){
			$this->error("请输入红包寄语");
		}
		if(empty($_POST['shuliang'])){
			$this->error("请输入发送多少个红包");
		}
		if(!is_numeric($_POST['num'])&&!is_numeric($_POST['money'])&& !is_numeric($_POST['shuliang'])){
			$this->error("您输入有误");
		}
		$data['num']=$_POST['num'];
		$data['money']=$_POST['money'];
		$data['content']=$_POST['content'];
		$data['add_time']=time();
		$data['type']=1;
		$data['status']=1;
		$shuliang=$_POST['shuliang'];
		
		for ($x=1; $x<=$shuliang; $x++) {
 			
			$re[]=M("Packet")->add($data);
		
		}
		if(!in_array($re,false)){
			$this->success("发放成功");
		}else{
			$this->error("发放失败");
		}
		
	}
    
}